package com.hbnu;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class WeatherDAOImpl implements WeatherDAO {
    @Override
    public void addWeather(Weather weather) {
        String sql = "INSERT INTO weather_data (city, temperature, humidity, date) VALUES (?, ?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, weather.getCity());
            stmt.setDouble(2, weather.getTemperature());
            stmt.setInt(3, weather.getHumidity());
            stmt.setString(4, weather.getDate());
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.err.println("添加天气记录失败！");
            e.printStackTrace();
        }
    }

    @Override
    public List<Weather> getWeatherByCity(String city) {
        List<Weather> result = new ArrayList<>();
        String sql = "SELECT * FROM weather_data WHERE city = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, city);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                Weather weather = new Weather(
                        rs.getString("city"),
                        rs.getDouble("temperature"),
                        rs.getInt("humidity"),
                        rs.getString("date")
                );
                result.add(weather);
            }
        } catch (SQLException e) {
            System.err.println("查询天气记录失败！");
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public boolean deleteWeatherByCity(String city) {
        String sql = "DELETE FROM weather_data WHERE city = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, city);
            int rowsAffected = stmt.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
            System.err.println("删除天气记录失败！");
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<Weather> getAllWeather() {
        List<Weather> result = new ArrayList<>();
        String sql = "SELECT * FROM weather_data";
        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                Weather weather = new Weather(
                        rs.getString("city"),
                        rs.getDouble("temperature"),
                        rs.getInt("humidity"),
                        rs.getString("date")
                );
                result.add(weather);
            }
        } catch (SQLException e) {
            System.err.println("获取所有天气记录失败！");
            e.printStackTrace();
        }
        return result;
    }
}